package com.sttk.fastMark.camera.fragment;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ImageUtils;
import com.sttk.fastMark.camera.BaseEditFragment;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.activity.PicEditActivity;
import com.sttk.fastMark.camera.basic.imgedit.Matrix3;
import com.sttk.fastMark.camera.basic.utils.RxUtil;
import com.sttk.fastMark.camera.databinding.FragmentPaintBinding;
import com.vilyever.drawingview.DrawingView;
import com.vilyever.drawingview.brush.drawing.CircleBrush;
import com.vilyever.drawingview.brush.drawing.PenBrush;
import com.vilyever.drawingview.brush.drawing.RectangleBrush;
import com.vilyever.drawingview.brush.drawing.ShapeBrush;


import io.reactivex.rxjava3.core.ObservableEmitter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PaintFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PaintFragment extends BaseEditFragment<FragmentPaintBinding> implements View.OnClickListener {

    public static final int INDEX = 6;
    private DrawingView mPaintView;
    private PenBrush penBrush;
    private ShapeBrush shapeBrush;

    private int color = Color.parseColor("#FF0000");
    private int paintWidth = 15;

    public static PaintFragment newInstance() {
        PaintFragment fragment = new PaintFragment();
        return fragment;
    }

    @Override
    public void onShow() {
        mImgEditActivity.mode = PicEditActivity.MODE_PAINT;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
        mPaintView.setVisibility(View.VISIBLE);
        mPaintView.clear();
        mPaintView.setBrush(penBrush);
        clearSelection();

        mPaintView.setUndoRedoStateDelegate(new DrawingView.UndoRedoStateDelegate() {
            @Override
            public void onUndoRedoStateChange(DrawingView drawingView, boolean canUndo, boolean canRedo) {
                if (canUndo) {
                    mDataBinding.ivDrawRevocation.setImageResource(R.drawable.achehui1);
                } else {
                    mDataBinding.ivDrawRevocation.setImageResource(R.drawable.achehui);
                }
                if (canRedo) {
                    mDataBinding.ivDrawRecover.setImageResource(R.drawable.aqianjin1);
                } else {
                    mDataBinding.ivDrawRecover.setImageResource(R.drawable.aqianjin);
                }
            }
        });
    }

    @Override
    public void backToMain() {
        mImgEditActivity.mode = PicEditActivity.MODE_NONE;
        mImgEditActivity.mImgView.setVisibility(View.VISIBLE);
        mPaintView.setVisibility(View.GONE);
        mPaintView.clear();
        mImgEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_paint;
    }

    @Override
    protected void initView() {
        mPaintView = mImgEditActivity.mPaintView;

        penBrush = PenBrush.defaultBrush();
        penBrush.setColor(color);//默认画笔颜色
        penBrush.setSize(paintWidth);//默认画笔大小

        shapeBrush = RectangleBrush.defaultBrush();
        shapeBrush.setColor(color);//默认画笔颜色
        shapeBrush.setSize(paintWidth);//默认画笔大小
        onShow();

        mDataBinding.ivDrawRecover.setOnClickListener(this);
        mDataBinding.ivDrawRevocation.setOnClickListener(this);
        mDataBinding.llPaint.setOnClickListener(this);
        mDataBinding.llArrow.setOnClickListener(this);
        mDataBinding.llRectangle.setOnClickListener(this);
        mDataBinding.llCircle.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        clearSelection();
        if (id == R.id.llPaint) {//画笔
            mDataBinding.ivPaint.setImageResource(R.drawable.ahuabi1);
            mDataBinding.tvPaint.setTextColor(Color.parseColor("#664DE8"));
            mPaintView.setBrush(penBrush);
        } else if (id == R.id.llRectangle) {//矩形
            mDataBinding.ivRectangle.setImageResource(R.drawable.ajuxing1);
            mDataBinding.tvRectangle.setTextColor(Color.parseColor("#664DE8"));
            shapeBrush = RectangleBrush.defaultBrush();
            shapeBrush.setColor(color);//默认画笔颜色
            mPaintView.setBrush(shapeBrush);
        } else if (id == R.id.llCircle) {//圆形
            mDataBinding.ivCircle.setImageResource(R.drawable.ayuan1);
            mDataBinding.tvCircle.setTextColor(Color.parseColor("#664DE8"));
            shapeBrush = CircleBrush.defaultBrush();
            shapeBrush.setColor(color);//默认画笔颜色
            mPaintView.setBrush(shapeBrush);
        }else if (id == R.id.ivDrawRecover) {//复原
            mPaintView.redo();
        } else if (id == R.id.ivDrawRevocation) {//撤回
            mPaintView.undo();
        }
    }

    @Override
    protected void initData() {

    }


    /**
     * 保存涂鸦
     */
    public void applyPaintImage() {
        showDialog(getString(R.string.handle_ing));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Matrix touchMatrix = mImgEditActivity.mImgView.getImageViewMatrix();

                Bitmap retBitmap = Bitmap.createBitmap(mImgEditActivity.getMainBit()).copy(
                        Bitmap.Config.ARGB_8888, true);
                Canvas canvas = new Canvas(retBitmap);

                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());

                float[] f = new float[9];
                m.getValues(f);
                int dx = (int) f[Matrix.MTRANS_X];
                int dy = (int) f[Matrix.MTRANS_Y];
                float scale_x = f[Matrix.MSCALE_X];
                float scale_y = f[Matrix.MSCALE_Y];
                canvas.save();
                canvas.translate(dx, dy);
                canvas.scale(scale_x, scale_y);

                Bitmap paintBitmap = ImageUtils.view2Bitmap(mPaintView);

                if (paintBitmap != null) {
                    canvas.drawBitmap(paintBitmap, 0, 0, null);
                }
                canvas.restore();

                emitter.onNext(retBitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap != null) {
                    mImgEditActivity.changeMainBitmap(bitmap, true);
                }
                mPaintView.clear();
                backToMain();
            }
        });
    }

    private void clearSelection() {
        mDataBinding.ivPaint.setImageResource(R.drawable.ahuabi);
        mDataBinding.tvPaint.setTextColor(Color.parseColor("#000000"));
        mDataBinding.ivArrow.setImageResource(R.drawable.ajiantou);
        mDataBinding.tvArrow.setTextColor(Color.parseColor("#000000"));
        mDataBinding.ivRectangle.setImageResource(R.drawable.ajuxing);
        mDataBinding.tvRectangle.setTextColor(Color.parseColor("#000000"));
        mDataBinding.ivCircle.setImageResource(R.drawable.ayuan);
        mDataBinding.tvCircle.setTextColor(Color.parseColor("#000000"));
    }
}